package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.common.BCrypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null) return paramError;
        Page<AdSensitive> sensitivePage = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdSensitive> adSensitiveLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getName())) {
            adSensitiveLambdaQueryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }

        return getPageResult(dto, sensitivePage, adSensitiveLambdaQueryWrapper);
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) return paramError;
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //判断参数
        if (adSensitive == null || adSensitive.getId() == null) return paramError;
        updateById(adSensitive);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id == null) return paramError;
        AdSensitive adSensitive = getById(id);
        if (adSensitive == null) return paramError;
        removeById(id);
        return ResponseResult.okResult("删除成功");
    }

    //md5手动加密
    public static void main(String[] args) {
      /*  String salt="./hli9-:!`1120P";
        String s = DigestUtils.md5DigestAsHex(("2112"+salt).getBytes());
        System.out.println("输出："+s);//4b3464c810fd22462d89907390add837*/
        String gensalt = BCrypt.gensalt();//这个是盐  29个字符，随机生成
        System.out.println(gensalt);
        String password = BCrypt.hashpw("123456", gensalt);  //根据盐对密码进行加密 //$2a$10$SdevcdO1FSqh3upPAgpIsuTCc9bSF2BsyQ/kRFt8Muw.mtbReKaOi
        System.out.println(password);//加密后的字符串前29位就是盐

        boolean checkpw = BCrypt.checkpw("123456", password);
        System.out.println(checkpw);

    }
}
